package com.example.easybuy.service;

import com.example.easybuy.dao.ShoppingCarDao;
import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.ShoppingCar;
import com.example.easybuy.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {
    private Logger logger = Logger.getLogger(ShoppingCarServiceImpl.class);

    @Autowired
    private ShoppingCarDao shoppingCarDao;


    /**
     * 添加购物车
     * 数量
     * 库存
     *
     * @param gid
     * @param uid
     * @param proNum
     * @return
     */
    @Override
    public String addShoppingCar(String gid, String uid, String proNum) {
        logger.debug("ShoppingCarServiceImpl addShoppingCar  gid: " + gid + " uid: " + uid + " proNum: " + proNum);
        ShoppingCar shoppingCar = new ShoppingCar();
        String isSuccess = "";
        try {
            int gids =0;
            if (!StringUtils.isEmpty(gid)) {
                 gids = Integer.parseInt(gid);
                shoppingCar.setProductsId(gids);
            }


            if (!StringUtils.isEmpty(uid)) {
                int uids = Integer.parseInt(uid);
                shoppingCar.setUserId(uids);
            }
            if (!StringUtils.isEmpty(proNum)) {

                int proNums = Integer.parseInt(proNum);
                shoppingCar.setProNum(proNums + 1);
            }

            int addCount = shoppingCarDao.addShoppingCar(shoppingCar);

            logger.debug("ShoppingCarServiceImpl addShoppingCar  addCount: " + addCount);
            if (addCount > 0) {
                isSuccess = "200";
            } else {
                isSuccess = "500";
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("ShoppingCarServiceImpl addShoppingCar  gid: " + gid + " uid: " + uid + " proNum: " + proNum + " is error ", e);
        }
        return isSuccess;
    }

    /**
     * 根据用户id查询下面的购物车商品
     *
     * @param
     * @return
     */
    public List<Products> shoppingCarUserById(String uids) {
        logger.debug("ShoppingCarServiceImpl  shoppingCarUserById  uids: " + uids);
        List<Products> productsList = null;
        int uid = 0;
        try {
            if (!StringUtils.isEmpty(uids)) {
                uid = Integer.parseInt(uids);
                System.out.println("用户id： " + uid);
            }
            System.out.println("用户id： " + uid);
            productsList = shoppingCarDao.shoppingCarUserById(uid);
            logger.debug("ShoppingCarServiceImpl  shoppingCarUserById  uid: " + uid + " result: " + productsList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("ShoppingCarServiceImpl  shoppingCarUserById  uid: " + uid + " is error ", e);
        }
        return productsList;
    }


    /**
     * 根据用户id、根据购物车的商品id
     * 查 当前用户下一个购物车的数量
     */
    public int proNum(String productsId, String uid) {
        logger.debug("ShoppingCarServiceImpl  proNum  productsId: " + productsId + " uid: " + uid);
        try {
            int productsIds = 0;
            if (!StringUtils.isEmpty(productsId)) {
                productsIds = Integer.parseInt(productsId);
            }

            int uids = 0;
            if (!StringUtils.isEmpty(uid)) {
                uids = Integer.parseInt(uid);
            }
            int proNumCount = shoppingCarDao.proNum(productsIds, uids);
            logger.debug("ShoppingCarServiceImpl  proNum  productsId: " + productsId + " uid: " + uid + " proNumCount: " + proNumCount);
            return proNumCount;
        } catch (Exception e) {
            e.printStackTrace();
            logger.debug("ShoppingCarServiceImpl  proNum  productsId: " + productsId + " uid: " + uid + " is error : ", e);
        }
        return 0;
    }
}

